package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.BrandDao;
import com.aigou.admin.entity.Brand;
import org.springframework.stereotype.Repository;


import java.util.List;

/**
 * @author chai
 * @date 2024/12/4 0004
 * @Description
 */
@Repository
public class BrandDaoImpl implements BrandDao {


    @Override
    public List<Brand> selectAll() {
        return JDBCUtils.executeQuery(Brand.class, "select * from s_brand");
    }

    @Override
    public Brand selectById(String id) {
        List<Brand> brands = JDBCUtils.executeQuery(Brand.class, "select * from s_brand where id = ? ",  id);
        if (brands.isEmpty()) {
            return null;
        }
        return brands.get(0);
    }

    @Override
    public Boolean insert(Brand brand) {
        int rows = JDBCUtils.executeUpdate("insert into s_brand(id,brand_name,brand_type,brand_img) values(?,?,?,?)",brand.getId(),brand.getBrand_name(),brand.getBrand_type(),brand.getBrand_img());
        return rows > 0;
    }

    @Override
    public Boolean update(Brand brand) {
        int rows = JDBCUtils.executeUpdate("update s_brand set brand_name=?,brand_type=?,brand_img=? where id = ?",brand.getBrand_name(),brand.getBrand_type(),brand.getBrand_img(), brand.getId());
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_brand where id = ?", id);
        return rows >0;
    }

    @Override
    public Brand selectByBrand(String brand) {
        List<Brand> brands=JDBCUtils.executeQuery(Brand.class,"select * from s_brand=?",brand);
        if(brands.isEmpty()){
            return null;
        }
        return brands.get(0);
    }

    @Override
    public Integer selectCount() {
      return JDBCUtils.executeCount("select count(*) from s_brand");
    }

    @Override
    public List<Brand> selectBrandByLimit(Integer start, Integer limit) {
        return JDBCUtils.executeQuery(Brand.class,"select * from s_brand limit ?,?",start,limit);
    }
}
